package tenants

import (
	"context"
	"errors"
	"product/common/validator"
	"strconv"

	"product/authorization/serve/internal/svc"
	"product/authorization/serve/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetTenantByWebsiteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 查询租户列表
func NewGetTenantByWebsiteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTenantByWebsiteLogic {
	return &GetTenantByWebsiteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetTenantByWebsiteLogic) GetTenantByWebsite(req *types.GetTenantByWebsiteReq) (resp *types.GetTenantByWebsiteResp, err error) {
	if validator.IsEmptyString(req.Website) {
		return nil, errors.New("请输入租户网站域名")
	}
	tenant, err := l.svcCtx.TenantModel.FindTenantByWebsite(l.ctx, req.Website)
	if err != nil {
		return nil, errors.New("没有租户信息")
	}
	return &types.GetTenantByWebsiteResp{
		Id:      strconv.FormatInt(tenant.ID, 10),
		Message: "查询成功",
		Name:    tenant.Name,
		Ok:      true,
	}, nil
}
